package com.ficus.road.maintain.handler.monitor;

import com.ficus.road.maintain.core.model.Car;
import com.ficus.road.maintain.dto.Checklist;
import com.ficus.road.maintain.dto.ChecklistOfCar;
import com.ficus.road.maintain.dto.PushStatistic;
import com.ficus.road.maintain.dto.TotalStatistic;
import com.ficus.road.maintain.util.MyDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;


/**
 * @author xiawen yang
 * @date 2022/5/23 下午5:09
 */
@Component
@Order(0)
@Slf4j
public class InitChecklistHandler extends AbstractMonitorHandler {


    @Override
    Checklist doFilter(Checklist checklist) {
        Checklist initChecklist = initChecklist();
        checklistDao.saveOrUpdate(initChecklist);
        log.info("巡检报表初始化成功");
        return initChecklist;
    }

    public Checklist initChecklist() {
        Date today = new Date();
        String todayStr = MyDateUtil.dateFormatStr(today, "yyyy-MM-dd");
        String yesterdayDateStr = MyDateUtil.dateFormatStr(DateUtils.addDays(today, -1), "yyyy-MM-dd");
        Checklist checklist = new Checklist();
        checklist.setDate(todayStr);

        Checklist yesterdayFetch = checklistDao.fetch(yesterdayDateStr);
        List<Car> allCars = carDao.findAll();
        List<ChecklistOfCar> checklistOfCars = new ArrayList<>();
        //初始化carCheckList
        allCars.stream().forEach(car -> {
            List<ChecklistOfCar> yesterdayChecklist = null;
            Double yesterdayStorageCap = 0d;
            ChecklistOfCar yesterdayCheck = null;
            if (ObjectUtils.isNotEmpty(yesterdayFetch)) {
                yesterdayChecklist = yesterdayFetch.getCarList();
                Optional<ChecklistOfCar> yesterdayCheckOptional = yesterdayChecklist.stream().filter(yesterday -> yesterday.getCarId().equals(car.getId())).findFirst();
                if (yesterdayCheckOptional.isPresent()) {
                    yesterdayCheck = yesterdayCheckOptional.get();
                }
            }
            yesterdayStorageCap = (yesterdayCheck == null || yesterdayCheck.getStorageDiskCap() == null) ? 0 : yesterdayCheck.getStorageDiskCap();
            ChecklistOfCar todayCheck = ChecklistOfCar.builder().carId(car.getId()).plateNo(car.getPlateNo()).date(todayStr).version("0.0")
                    .sdkCarTotalMinute(0).cloudCarTotalMinute(0).mergeNum(0l).sdkCarStatus(0).cloudCarStatus(0).isCarOutStatusError(0)
                    .isGNSSError(0).isLidarError(0).isCameraError(0).storageDiskCap(yesterdayStorageCap)
                    .gNSSErrorTimes(0).lidarErrorTimes(0).cameraErrorTimes(0).miles(0d)
                    .hitNum(0l).uncheckHitNum(0l).checkRate(0d).pushThirdNum(0l).passHitNum(0l).passRate(0d).build();
            checklistOfCars.add(todayCheck);
        });
        checklist.setCarList(checklistOfCars);
        //初始化推送统计
        checklist.setPushStatistic(PushStatistic.builder().date(todayStr)
                .push2ThirdPartySuccess(0).push2ThirdPartyFail(0).push2ThirdPartySuccessRate(0d)
                .push2YtSuccess(0).push2YtFail(0).push2YtSuccessRate(0d).build());
        //初始化总数统计
        checklist.setTotalStatistic(TotalStatistic.builder().totalRuntime(0d).totalNewHits(0)
                .totalMiles(0d).totalCheckHits(0).build());
        return checklist;
    }
}
